//
//  TumblrDownloader.m
//  ReactIcons
//
//  Created by Nong Nghia on 5/21/12.
//  Copyright (c) 2012 CMC Software Solution Company Ltd. All rights reserved.
//
    
#import "TumblrDownloader.h"
#import "JSON.h"
#import "DownloadHelper.h"

#define HOST @"http://www.reacticons.com/api/read/json?type=photo"
#define ARG_TAGGED @"&tagged="
#define ARG_START @"&start="
#define ARG_QUANTITY @"&num="
#define MAX_QUANTITY 50

@implementation TumblrDownloader

-(void)downloadFromTumblrWithTag:(NSString*)tag{
    helper=[[DownloadHelper alloc] init];
    itemList=[[NSMutableArray alloc] init];
    NSString * urlString;
    [[NSUserDefaults standardUserDefaults] setValue:tag forKey:@"TAG_DOWNLOADING"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    if ([tag isEqualToString:@"ALL"]) {
         urlString=[NSString stringWithFormat:@"%@%@0%@50",HOST,ARG_START,ARG_QUANTITY];
    }
    else {
        urlString=[NSString stringWithFormat:@"%@%@%@%@0%@%d",HOST,ARG_TAGGED,tag,ARG_START,ARG_QUANTITY,MAX_QUANTITY];

    }
    if (rHelper==nil) {
        rHelper=[[TumblrRequestHelper alloc] init];
    }
    kNotifiString=[rHelper getListPostWithURL:urlString];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTumblrImageListComplete:) name:kNotifiString object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTumblrImageListFailed:) name:@"kRequestListFailedNotification" object:nil];
    
}


#pragma mark - request delegate

- (void)getTumblrImageListComplete:(NSNotification *)notif 
{

    NSLog(@"TumblrDownloader - getTumblrImageListComplete - %d",itemList.count);
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kNotifiString object:nil];

    NSString* newStr = [[notif object] substringFromIndex:22];
    //remove ";"
    newStr=[newStr substringToIndex:newStr.length-2];
    
    NSMutableDictionary *mudic=[[NSMutableDictionary alloc] initWithDictionary:[newStr JSONValue]];
    [[NSUserDefaults standardUserDefaults] setValue:[mudic objectForKey:@"posts-total"] forKey:@"NumberOfTumblrPosts"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSMutableArray * arr=[mudic objectForKey:@"posts"];
    
    //
    
    //------ADD LIST INFO HERE-----
    
    //------POST NOTIFY TO UI------
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kNeedUpdateImageListNotification" object:nil];
    
    //------DOWNLOAD IMAGE FOR ADDTION LIST---
    for (NSMutableDictionary *dic in arr) {
        [itemList addObject:dic];
        //[helper downloadFile:[[dic objectForKey:@"photo-url-1280"] lastPathComponent] url:[NSURL URLWithString:[dic objectForKey:@"photo-url-1280"]] progressView:nil];
    }
    
    if (arr.count == MAX_QUANTITY) {
        
        NSString * urlString;
        NSString *tag=[[NSUserDefaults standardUserDefaults] objectForKey:@"TAG_DOWNLOADING"];
        int start=[[mudic objectForKey:@"posts-start"] intValue]+MAX_QUANTITY;
        if ([tag isEqualToString:@"ALL"]) {
            urlString=[NSString stringWithFormat:@"%@%@%d%@%d",HOST,ARG_START,start,ARG_QUANTITY,MAX_QUANTITY];
        }
        else {
            urlString=[NSString stringWithFormat:@"%@%@%@%@%d%@%d",HOST,ARG_TAGGED,tag,ARG_START,start,ARG_QUANTITY,MAX_QUANTITY];
            
        }
//            NSLog(@"urltring:%@",urlString);
        kNotifiString=[rHelper getListPostWithURL:urlString];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTumblrImageListComplete:) name:kNotifiString object:nil];

    }
    else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kDownloadListFromTumblrSuccessNotification" object:itemList];
        
        [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kRequestListFailedNotification" object:nil];

    }
}

- (void)getTumblrImageListFailed:(NSNotification *)notif {
    
    NSLog(@"TumblrDownloader - getTumblrImageListFailed - ");

    [[NSNotificationCenter defaultCenter] removeObserver:self name:kNotifiString object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kRequestListFailedNotification" object:nil];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kDownloadListFromTumblrFailedNotification" object:[notif object]];

}



@end
